import {toast} from "../../../utils/tool";

const app = getApp()
Component({
    properties: {
        defaultIndex: {
            type: Number,
        },
        currentTab: {
            type: Number,
            observer: function (ev) {
                if (ev === this.data.defaultIndex) {
                    this.initData(true)
                }
            }
        },
        type: {
            type: Number,
        }
    },
    data: {
        page: 1,
        hasMore: true,
        list: [],
    },
    pageLifetimes: {
        show() {
            const {defaultIndex, currentTab} = this.data
            if (defaultIndex !== currentTab) return
            this.initData(true)
        },
    },
    methods: {
        opening_code(ev) {
            const {id} = ev.currentTarget.dataset;
            this.triggerEvent('opening', id)
        },
        unsettled(ev) {
            const {id} = ev.currentTarget.dataset
            this.triggerEvent('unsettled', id)
        },
        openDoorSmall(ev) {
            const {status, id} = ev.currentTarget.dataset
            console.log(status, id)
            this.triggerEvent('open', {status, id})
        },
        cancelOrder(ev) {
            const {id} = ev.currentTarget.dataset
            this.triggerEvent('cancel', id)
        },
        close(ev) {
            const {id} = ev.currentTarget.dataset
            this.triggerEvent('close', id)
        },
        initData(show = false) {
            const {type, page} = this.data
            if (show) {
                wx.showLoading({title: "正在获取数据..."})
            }
            app.ajax({
                url: '/api/user/orderList',
                data: {
                    status: type,
                    page: page
                },
                success: resp => {
                    const {data} = resp
                    const list = data && data.length ? data : []
                    this.setData({list})
                    if (!show) return
                    setTimeout(() => {
                        wx.hideLoading()
                    }, 400)
                }
            })
        },
    }
});
